package com.bcn.login_mybatisplus_demo.weight.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.bcn.login_mybatisplus_demo.constant.HttpStatus;
import com.bcn.login_mybatisplus_demo.interceptor.AuthStorage;
import com.bcn.login_mybatisplus_demo.pojo.vo.R;
import com.bcn.login_mybatisplus_demo.util.Convert;
import com.bcn.login_mybatisplus_demo.util.DateUtils;
import com.bcn.login_mybatisplus_demo.util.StringUtils;
import com.bcn.login_mybatisplus_demo.weight.domain.TAppWeight;
import com.bcn.login_mybatisplus_demo.weight.mapper.TAppWeightMapper;
import com.bcn.login_mybatisplus_demo.weight.service.ITAppWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author administrator
 * @date 2025-03-11
 */
@Service
public class TAppWeightServiceImpl implements ITAppWeightService
{
    @Autowired
    private TAppWeightMapper tAppWeightMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param weightId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public TAppWeight selectTAppWeightById(Long weightId)
    {
        return tAppWeightMapper.selectTAppWeightById(weightId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param tAppWeight 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TAppWeight> selectTAppWeightList(TAppWeight tAppWeight)
    {
        tAppWeight.setUserId(AuthStorage.getUser().getId());

        return tAppWeightMapper.selectTAppWeightList(tAppWeight);
    }

    @Override
    public List<TAppWeight> selectTAppWeightLists(TAppWeight tAppWeight) {

        return Collections.emptyList();
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param tAppWeight 【请填写功能名称】
     * @return 结果
     */
    @Override
    public R insertTAppWeight(TAppWeight tAppWeight)
    {
        tAppWeight.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        tAppWeight.setUserId(AuthStorage.getUser().getId());

        return R.success(tAppWeightMapper.insertTAppWeight(tAppWeight));

    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param tAppWeight 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTAppWeight(TAppWeight tAppWeight)
    {
        return tAppWeightMapper.updateTAppWeight(tAppWeight);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTAppWeightByIds(String ids)
    {
        return tAppWeightMapper.deleteTAppWeightByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param weightId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteTAppWeightById(Long weightId)
    {
        return tAppWeightMapper.deleteTAppWeightById(weightId);
    }

    @Override
    public R getWeightChange(TAppWeight weight) {
        Long id = AuthStorage.getUser().getId();
        weight.setUserId(id);
        List<TAppWeight> tAppWeights = tAppWeightMapper.selectTAppWeightList(weight);
        if (tAppWeights.isEmpty()) {
            return R.error(HttpStatus.CONFLICT,"没有记录体重");
        }
        String weightRecord = weight.getWeightRecord();
        if (StringUtils.isEmpty(weightRecord)) {
            return R.error(HttpStatus.CONFLICT,"体重不能为空");
        }
        double thisWeightNumber = Double.parseDouble(weightRecord);
        weight.setWeightRecordDate(DateUtils.getDate());
        TAppWeight tAppWeight = tAppWeightMapper.selectTAppMaxWeight(weight);
        if (tAppWeight == null) {
            return R.error(HttpStatus.CONFLICT,"没有记录体重");
        }
        double WeightNumber = Double.parseDouble(tAppWeight.getWeightRecord());
        return R.success(thisWeightNumber - WeightNumber);
    }

    @Override
    public R getBMI(TAppWeight weight) {
        if (StringUtils.isEmpty( weight.getWeightRecord()) || StringUtils.isEmpty( weight.getHeight()) ) {
            return R.error("请输入身高体重");
        }


        // 计算 BMI
        double bmi = Double.parseDouble(weight.getWeightRecord()) / (Double.parseDouble(weight.getHeight()) * Double.parseDouble(weight.getHeight()));
        bmi = bmi*10000;
        // 输出 BMI 值
        System.out.println("您的BMI值是: " + bmi);

        Map<String,Object> hash  = new HashMap();
        hash.put("bmi", bmi);
        String msg = "";


        // 根据BMI值判断体重分类
        if (bmi < 18.5) {
            msg = "体重过轻";
        } else if (bmi >= 18.5 && bmi < 24.9) {
            msg = "正常范围";
        } else if (bmi >= 25 && bmi < 29.9) {
            msg = "超重";
        } else {
            msg = "肥胖";
        }
        hash.put("msg",msg);
        return R.success(hash);
    }

    @Override
    public List<TAppWeight> getReportDate(TAppWeight tAppWeight) {
        List<TAppWeight> tAppWeights = tAppWeightMapper.selectTAppWeightRepoetList(tAppWeight.getNumber());
        return tAppWeights;
    }

}
